﻿@using Smartstore.Web.Models.Orders;
@using Smartstore.Core.Catalog.Products;

@model OrderDetailsModel

@{
    Assets.AppendTitleParts(T("PageTitle.OrderDetails"));
    Assets.BodyAttributes.AppendCssClass("content-overflow-initial");

    var bundleItemThumbSize = Math.Min(Model.BundleThumbSize, 60);
    var hasOrderDetails1 = Model.BillingAddress != null || (Model.IsShippable && Model.ShippingAddress != null);
    var hasOrderDetails2 = Model.PaymentMethod.HasValue() || Model.IsShippable || Model.VatNumber.HasValue();

    async Task RenderAddress(AddressModel address)
    {
        <div class="mb-2">
            @Html.Raw(address.FormattedAddress)
        </div>
        <div sm-if="address.Email.HasValue()" class="email">
            @T("Order.Email"): @address.Email
        </div>
        <div sm-if="address.PhoneEnabled && address.PhoneNumber.HasValue()" class="phone">
            @T("Order.Phone"): @address.PhoneNumber
        </div>
        <div sm-if="address.FaxEnabled && address.FaxNumber.HasValue()" class="fax">
            @T("Order.Fax"): @address.FaxNumber
        </div>
    }

    async Task SimpleProduct(OrderDetailsModel.OrderItemModel item)
    {
        <div class="cart-row">
            <div class="row">
                @* Image *@
                <div class="cart-item-img col-3 col-xl-2" sm-if="Model.ShowProductImages && item.Image != null && item.Image.HasImage()">
                    <img sm-model="item.Image" class="img-fluid" />
                </div>

                @* Data *@
                <div class="cart-item-data col">

                    @* Title *@
                    <div class="cart-item-group">
                        <div class="cart-item-title">
                            @if (item.ProductUrl.HasValue())
                            {
                                <a class="cart-item-link" href="@item.ProductUrl" title="@T("Products.Details")" sm-language-attributes-for="item.ProductName">@item.ProductName</a>
                            }
                            else
                            {
                                <span sm-language-attributes-for="item.ProductName">@item.ProductName</span>
                            }
                        </div>
                    </div>

                    @* Attributes & Variants *@
                    <div class="cart-item-attrs cart-item-group fs-sm" sm-suppress-if-empty>
                        @if (Model.ShowSku && item.Sku.HasValue())
                        {
                            <span>@T("Order.Product(s).SKU"):</span>
                            <span>@item.Sku</span>
                        }
                        @if (item.AttributeInfo.HasValue())
                        {
                            @Html.Raw(item.AttributeInfo)
                        }
                    </div>

                    @* Single unit price *@
                    <div class="pd-finalprice">
                        <span class="pd-finalprice-amount">
                            @Html.Raw(item.UnitPrice)
                        </span>
                    </div>

                    @* Quantity *@
                    <div class="cart-item-qty pd-muted">
                        <span>@T("Order.Product(s).Quantity"):</span> <span>@item.Quantity @item.QuantityUnit</span>
                    </div>

                    @* Subtotal *@
                    <div sm-if="item.UnitPrice != item.SubTotal" class="pd-subtotal pd-muted">
                        <span class="pd-subtotal-label">@T("Order.Product(s).Total"):</span>
                        <span class="pd-subtotal-amount fwm">
                            @Html.Raw(item.SubTotal)
                        </span>
                    </div>
                </div>
            </div>
        </div>

        <zone name="orderdetails_page_after_item" model="item.Id" />
    }

    async Task BundleProduct(OrderDetailsModel.BundleItemModel item, OrderDetailsModel.OrderItemModel parentItem, int index)
    {
        <div class="cart-row cart-row-child">
            <div class="row">
                @* Spacer for image *@
                <div class="cart-item-img col-3 col-xl-2 d-none d-md-flex d-lg-none d-xl-flex text-start">
                    @if (index == 0)
                    {
                        <span class="fwm pt-1">@T("Products.Bundle.BundleIncludes"):</span>
                    }
                    else
                    {
                        <span>&nbsp;</span>
                    }
                </div>

                @* Data *@
                <div class="cart-item-data col">
                    @* Title & Description *@
                    <div class="cart-item-group">
                        @* Title *@
                        <div class="cart-item-title">
                            @if (Model.ShowProductBundleImages)
                            {
                                <span class="cart-item-bundle-img mr-1" style="width: @(bundleItemThumbSize)px; height: @(bundleItemThumbSize)px;">
                                    @if (item.Image != null && item.Image.HasImage() && !item.HideThumbnail)
                                    {
                                        <img sm-model="item.Image" class="img-fluid" />
                                    }
                                </span>
                            }

                            <span sm-if="item.Quantity > 1 && parentItem.BundlePerItemShoppingCart" class="badge badge-secondary">
                                @(item.Quantity)&nbsp;&#215;
                            </span>

                            @if (item.VisibleIndividually)
                            {
                                <a class="cart-item-link" href="@item.ProductUrl" title="@T("Products.Details")" sm-language-attributes-for="item.ProductName">
                                    @item.ProductName
                                </a>
                            }
                            else
                            {
                                <span sm-language-attributes-for="item.ProductName">@item.ProductName</span>
                            }
                        </div>
                    </div>

                    @* Attributes *@
                    <div sm-if="item.AttributeInfo.HasValue()" class="cart-item-attrs cart-item-group">
                        @Html.Raw(item.AttributeInfo)
                    </div>

                    @* Price *@
                    <div sm-if="item.PriceWithDiscount > 0" class="cart-item-price-group cart-item-group">
                        <div class="pd-finalprice">
                            <span class="pd-finalprice-amount">
                                @item.PriceWithDiscount
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <zone name="orderdetails_page_after_item" model="parentItem.Id" />
    }
}

<div class="page page-order-details">
    <div class="row mb-3">
        <div class="page-title mb-3 col">
            <h1 class="h2 mb-0">
                @T("Order.OrderDetails")
                <small class="text-muted"><small>@Model.OrderNumber</small></small>
            </h1>
        </div>

        <div class="print-buttons col col-auto">
            <a asp-action="Print" asp-route-id="@Model.Id" asp-route-pdf="false" target="print" class="btn btn-secondary btn-sm print-order-button" rel="nofollow">
                <i class="fa fa-print"></i>
                <span>@T("Order.Print")</span>
            </a>
            @if (Model.DisplayPdfInvoice)
            {
                <a asp-action="Print" asp-route-id="@Model.Id" asp-route-pdf="true" class="btn btn-secondary btn-sm pdf-order-button" rel="nofollow">
                    <i class="far fa-file-pdf"></i>
                    <span>@T("Order.GetPDFInvoice")</span>
                </a>
            }
        </div>
    </div>

    <div class="page-body">
        <zone name="orderdetails_page_top" />

        <div class="row mb-3">
            <div class="col-6 col-sm-auto pb-3">
                <h5 class="text-muted">@T("Order.OrderDate")</h5>
                <div>@Model.CreatedOn.ToShortDateString().ReplaceNativeDigits()</div>
            </div>
            <div class="col-6 col-sm-auto pb-3">
                <h5 class="text-muted">@T("Order.Order#")</h5>
                <div>@Model.OrderNumber</div>
            </div>
            <div sm-if="Model.DisplayPurchaseOrderNumber" class="col-6 col-sm-auto pb-3">
                <h5 class="text-muted">@T("Order.PurchaseOrderNumber")</h5>
                <div>@Model.PurchaseOrderNumber</div>
            </div>
            <div class="col-6 col-sm-auto pb-3">
                <h5 class="text-muted">@T("Order.OrderStatus")</h5>
                <div>@Model.OrderStatus</div>
            </div>
            <div class="col-6 col-sm-auto pb-3">
                <h5 class="text-muted">@T("Order.OrderTotal")</h5>
                <div class="text-success-emphasis fwm">@Model.OrderTotal</div>
            </div>
        </div>

        <zone name="orderdetails_page_overview" />

        <div sm-if="hasOrderDetails1 || hasOrderDetails2" class="card card-body order-details-box mb-4">
            <div class="row">
                <div sm-if="hasOrderDetails1" class="col-md-8">
                    <div class="row">
                        <div sm-if="Model.BillingAddress != null" class="col mb-4 mb-lg-0 billinginfo">
                            <h5>@T("Order.BillingAddress")</h5>
                            @{ await RenderAddress(Model.BillingAddress); }
                        </div>
                        <div sm-if="Model.IsShippable && Model.ShippingAddress != null" class="col mb-4 mb-lg-0 shippinginfo">
                            <h5>@T("Order.ShippingAddress")</h5>
                            @{ await RenderAddress(Model.ShippingAddress); }
                        </div>
                    </div>
                </div>

                <div sm-if="hasOrderDetails2" class="col-md-4">
                    <div class="row">
                        <div class="col">
                            @if (Model.IsShippable)
                            {
                                <h5>@T("Order.ShippingMethod")</h5>
                                <p>@Model.ShippingMethod</p>
                            }
                            @if (Model.PaymentMethod.HasValue())
                            {
                                <h5>@T("Order.PaymentMethod")</h5>
                                <p>@Model.PaymentMethod</p>

                                if (Model.CanRePostProcessPayment)
                                {
                                    <form asp-action="Details">
                                        <input type="submit" name="repost-payment" value="@T("Order.CompletePayment")" class="btn btn-primary btn-sm btn-block re-order-button btn-above" />
                                        <div class="small text-muted pt-2">@T("Order.CompletePayment.Hint")</div>
                                    </form>
                                }
                            }
                            @if (Model.VatNumber.HasValue())
                            {
                                <h5>@T("Order.VATNumber")</h5>
                                <p>@Model.VatNumber</p>
                            }
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <zone name="orderdetails_page_beforeproducts" />

        <div sm-if="Model.Items.Count > 0" id="order-items" class="row cart-container">

            @* Order Items *@
            <div class="col-lg-7 col-xl-8">
                <div id="cart-items" class="cart">
                    <div class="cart-body">
                        @foreach (var item in Model.Items)
                        {
                            if (item.ProductType == ProductType.BundledProduct)
                            {
                                await SimpleProduct(item);

                                if (item.BundleItems != null)
                                {
                                    var bundleItems = item.BundleItems.OrderBy(x => x.DisplayOrder).ToArray();
                                    for (var i = 0; i < bundleItems.Length; i++)
                                    {
                                        await BundleProduct(bundleItems[i], item, i);
                                    }
                                }
                            }
                            else
                            {
                                await SimpleProduct(item);
                            }
                        }
                    </div>
                </div>
            </div>

            @* Order summary *@
            <div class="col-lg-5 col-xl-4">
                <div class="cart-footer">
                    <div class="row">
                        <div sm-if="Model.CheckoutAttributeInfo.HasValue()" class="col-12 cart-footer-group mb-2">
                            <div class="cart-checkout-attrs">
                                @Html.Raw(Model.CheckoutAttributeInfo)
                            </div>
                        </div>
                        <div class="col-12 cart-footer-group">
                            <table class="cart-summary">
                                <tr class="cart-summary-subtotal">
                                    <td class="cart-summary-label">@T("Order.SubTotal"):</td>
                                    <td class="cart-summary-value">@Model.OrderSubtotal</td>
                                </tr>
                                <tr sm-if="Model.OrderSubTotalDiscount != 0" class="cart-summary-subtotaldiscount cart-summary-pos">
                                    <td class="cart-summary-label">@T("Order.SubTotalDiscount"):</td>
                                    <td class="cart-summary-value">@Model.OrderSubTotalDiscount</td>
                                </tr>
                                <tr sm-if="Model.IsShippable" class="cart-summary-shipping cart-summary-neg">
                                    <td class="cart-summary-label">@T("Order.Shipping"):</td>
                                    <td class="cart-summary-value">@Model.OrderShipping</td>
                                </tr>
                                <tr sm-if="Model.PaymentMethodAdditionalFee != 0" class="cart-summary-payfee cart-summary-neg">
                                    <td class="cart-summary-label">@T("Order.PaymentMethodAdditionalFee"):</td>
                                    <td class="cart-summary-value">@Model.PaymentMethodAdditionalFee</td>
                                </tr>
                                @if (Model.DisplayTaxRates && Model.TaxRates.Count > 0)
                                {
                                    foreach (var taxRate in Model.TaxRates)
                                    {
                                        <tr class="cart-summary-tax">
                                            <td class="cart-summary-label">@T("Order.TaxRateLine", taxRate.FormattedRate):</td>
                                            <td class="cart-summary-value">@taxRate.Amount.ToString()</td>
                                        </tr>
                                    }
                                }
                                <tr sm-if="Model.DisplayTax" class="cart-summary-tax">
                                    <td class="cart-summary-label">@T("Order.Tax"):</td>
                                    <td class="cart-summary-value">@Model.Tax</td>
                                </tr>
                                <tr sm-if="Model.OrderTotalDiscount != 0" class="cart-summary-totaldiscount cart-summary-pos">
                                    <td class="cart-summary-label">@T("Order.TotalDiscount"):</td>
                                    <td class="cart-summary-value">-@Model.OrderTotalDiscount</td>
                                </tr>

                                @if (Model.GiftCards.Count > 0)
                                {
                                    foreach (var gc in Model.GiftCards)
                                    {
                                        <tr class="cart-summary-giftcard">
                                            <td class="cart-summary-label">
                                                <span class="cart-summary-pos pr-1">@T("Order.GiftCardInfo", gc.CouponCode)</span>
                                                <span class="text-nowrap text-muted small">@T("ShoppingCart.Totals.GiftCardInfo.Remaining", gc.Remaining.ToString())</span>
                                            </td>
                                            <td class="cart-summary-value cart-summary-pos">-@gc.FormattedAmount</td>
                                        </tr>
                                    }
                                }

                                <tr sm-if="Model.RedeemedRewardPoints > 0" class="cart-summary-rewardpoints cart-summary-pos">
                                    <td class="cart-summary-label">@T("Order.RewardPoints", Model.RedeemedRewardPoints):</td>
                                    <td class="cart-summary-value">-@Model.RedeemedRewardPointsAmount</td>
                                </tr>
                                <tr sm-if="Model.CreditBalance > 0" class="cart-summary-credit-balance cart-summary-pos">
                                    <td class="cart-summary-label">@T("ShoppingCart.Totals.CreditBalance"):</td>
                                    <td class="cart-summary-value">-@Model.CreditBalance</td>
                                </tr>
                                <tr sm-if="Model.OrderTotalRounding != 0" class="cart-summary-totalrounding cart-summary-pos">
                                    <td class="cart-summary-label">@T("ShoppingCart.Totals.Rounding"):</td>
                                    <td class="cart-summary-value">-@Model.OrderTotalRounding</td>
                                </tr>
                                <tr>
                                    <td colspan="2"><hr class="my-0" /></td>
                                </tr>
                                <tr class="cart-summary-total">
                                    <td class="cart-summary-label">@T("Order.OrderTotal"):</td>
                                    <td class="cart-summary-value">@Model.OrderTotal</td>
                                </tr>
                            </table>

                            <zone name="orderdetails_page_aftertotal" />

                            <div sm-if="Model.Items.Count > 0" class="col-12 mt-2 cart-footer-actions" sm-suppress-if-empty>
                                <zone name="orderdetails_buttons_before" />

                                <a sm-if="Model.IsReturnRequestAllowed" asp-action="ReturnRequest" asp-controller="ReturnRequest" asp-route-id="@Model.Id" class="btn btn-secondary btn-block" rel="nofollow">
                                    <i class="fa fa-reply"></i>
                                    <span>@T("Order.ReturnItems")</span>
                                </a>

                                <a sm-if="Model.IsReOrderAllowed" asp-action="ReOrder" asp-route-id="@Model.Id" class="btn btn-primary btn-block" rel="nofollow">
                                    <i class="fal fa-shopping-cart"></i>
                                    <span>@T("Order.Reorder")</span>
                                </a>

                                <zone name="orderdetails_buttons_after" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>

        <zone name="orderdetails_page_afterproducts" />

        <div sm-if="Model.Shipments.Count > 0" class="card mt-4">
            <h5 class="card-header border-0">
                @T("Order.Shipments")
            </h5>

            <div class="shipments-box table-responsive">
                <table class="table mb-0">
                    <thead>
                        <tr>
                            <th>@T("Order.Shipments.ID")</th>
                            <th>@T("Order.Shipments.TrackingNumber")</th>
                            <th>@T("Order.Shipments.ShippedDate")</th>
                            <th>@T("Order.Shipments.DeliveryDate")</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model.Shipments)
                        {
                            <tr>
                                <td>
                                    <a asp-action="ShipmentDetails" asp-route-id="@item.Id" title="@T("Order.Shipments.ViewDetails")">
                                        @item.Id
                                    </a>
                                </td>
                                <td>
                                    @item.TrackingNumber
                                </td>
                                <td>
                                    @if (item.ShippedDate.HasValue)
                                    {
                                        @item.ShippedDate.Value.ToNativeString("D")
                                    }
                                    else
                                    {
                                        <span class="text-muted">@T("Order.Shipments.ShippedDate.NotYet")</span>
                                    }
                                </td>
                                <td>
                                    @if (item.DeliveryDate.HasValue)
                                    {
                                        @item.DeliveryDate.Value.ToNativeString("D")
                                    }
                                    else
                                    {
                                        <span class="text-muted">@T("Order.Shipments.DeliveryDate.NotYet")</span>
                                    }
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>

        <div sm-if="Model.OrderNotes.Count > 0" class="card mt-4">
            <h5 class="card-header border-0">
                @T("Order.Notes")
            </h5>

            <div class="ordernotes-box table-responsive">
                <table class="table mb-0">
                    <thead>
                        <tr>
                            <th>@T("Order.Notes.CreatedOn")</th>
                            <th>@T("Order.Notes.Note")</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model.OrderNotes)
                        {
                            <tr>
                                <td class="text-nowrap">@item.FriendlyCreatedOn</td>
                                <td dir="auto">@Html.Raw(item.Note)</td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>

        <zone name="orderdetails_page_bottom" />
    </div>
</div>

